package com.inossem.warehousemanage_tv.fragment;

import android.widget.GridView;
import android.widget.LinearLayout;

import com.inossem.warehousemanage_tv.R;
import com.inossem.warehousemanage_tv.adapter.PositionsAdapter;
import com.inossem.warehousemanage_tv.adapter.PositionsLayerAdapter;
import com.inossem.warehousemanage_tv.bean.BaseBean;
import com.inossem.warehousemanage_tv.bean.PositionsBean;
import com.inossem.warehousemanage_tv.utils.Api;
import com.inossem.warehousemanage_tv.utils.requestUtils.InossemCallback;
import com.inossem.warehousemanage_tv.utils.requestUtils.RetrofitUtils;

import retrofit2.Response;

/**
 * 仓库概览-仓位
 *
 * @author 詹建宇
 * @time on 2019/2/14 10:13
 * @email jianyu.zhan@inossem.com
 */
public class WarePositionsFragment extends BaseFragment {

    private LinearLayout layerLayout;
    private GridView gridView;

    @Override
    public int initLayout() {
        return R.layout.fragment_ware_positions;
    }

    @Override
    public void initView() {
        layerLayout = mView.findViewById(R.id.layerLayout);
        gridView = mView.findViewById(R.id.gridView);
    }

    @Override
    public void initData() {

        RetrofitUtils.getRetrofit(mActivity, "詹建宇", "刘宇", "仓库概览-仓位", "通过货架获取仓位")
                .create(Api.class)
                .getPositionList(mActivity.location_id, mActivity.area_id, mActivity.position_index1)
                .enqueue(new InossemCallback<BaseBean<PositionsBean>>() {
                    @Override
                    public void success(Response<BaseBean<PositionsBean>> response) {
                        PositionsBean bean = response.body().getBody();

                        if (null != bean.getLayer_list() && !bean.getLayer_list().isEmpty()) {
                            new PositionsLayerAdapter(mActivity, layerLayout, bean.getLayer_list(), (position, layerBean) -> setData(layerBean));

                            if (null != bean.getLayer_list().get(0).getPosition_list() && !bean.getLayer_list().get(0).getPosition_list().isEmpty()) {
                                setData(bean.getLayer_list().get(0));
                            }
                        }
                    }

                    @Override
                    public void failure(Response<BaseBean<PositionsBean>> response) {

                    }
                });
    }

    @Override
    public void initClick() {

    }

    private void setData(PositionsBean.LayerListBean layerBean) {
        PositionsAdapter positionsAdapter = new PositionsAdapter(mActivity, layerBean.getPosition_list());
        gridView.setAdapter(positionsAdapter);
        positionsAdapter.setListener((position, positionBean) -> {
            mActivity.position_id = positionBean.getPosition_id();
            mActivity.isTemporary = false;
            mActivity.startFragment(new WareMatsFragment());
        });
    }

}
